package org.springonrails.model;

import java.io.File;
import java.util.Properties;

import org.springonrails.appgen.ApplicationMavenStructure;
import org.springonrails.appgen.ApplicationStructure;
import org.springonrails.util.StringUtils;

/**
 * This class needs to be clean up.
 * @author LiuS
 *
 */
public class AppConfig 
{
	public static final String JDBC_PROP_DRIVER = "jdbc.driverClassName";
    public static final String JDBC_PROP_URL = "jdbc.url";
    public static final String JDBC_PROP_USER = "jdbc.username";
    public static final String JDBC_PROP_PASSWORD = "jdbc.password";
    
	public static final String OUTPUT_PACKAGE_NAME             ="outputPackageName" ;//            = "com.asksunny.demo";
	public static final String OUTPUT_PACKAGE_NAME_IN_FILEPATH = "outputPackageNameInFilePath" ; // = "com/asksunny/demo";	
	public static final String OUTPUT_DIR = "outputDir"; //                   = "target";
	public static final String PROJECT_NAME = "projectName"; //              = "demo";
	public static final String STRUCTURE_TYPE = "structureType";    //           = "maven";
	public static final String VIEW_TYPE = "viewType";    //                = "both";	
	public static final String DB_TYPE = "dbType"; //                      = "oracle";	
	public static final String SPRING_VERSION = "springVersion" ; // 			   = "1";
	public static final String CONTROLLER_TYPE = "controllerType"; //				= "multiple";
	
	public static final String ORM_TYPE = "ormType"; //				= "multiple";
	public static final String ORM_TYPE_IBATIS = "ibatis"; //				= "multiple";
	public static final String ORM_TYPE_HIBERNATE = "hibernate"; //				= "multiple";
	public static final String ORM_TYPE_EJB3 = "ejb3"; //				= "multiple";
	public static final String JEE_VERSION = "jeeVersion"; //		
	public static final String JEE_VERSION_5 = "5"; //		
	public static final String JEE_VERSION_1_5 = "1.5"; //
	public static final String JEE_VERSION_6 = "6"; //		
	public static final String JEE_VERSION_1_6 = "1.6"; //
	public static final String JEE_VERSION_7 = "7"; //		
	public static final String JEE_VERSION_1_7 = "1.7"; //
	public static final String JEE_VERSION_4 = "4"; //		
	public static final String JEE_VERSION_1_4 = "1.4"; //
	public static final String JEE_VERSION_3 = "3"; //		
	public static final String JEE_VERSION_1_3 = "1.3"; //
	
	public static final String OUTPUT_PACKAGE_NAME_DEFAULT             =  "com.asksunny.demo";
	public static final String OUTPUT_PACKAGE_NAME_IN_FILEPATH_DEFAULT  =  "com/asksunny/demo";	
	public static final String OUTPUT_DIR_DEFAULT  = "target";
	public static final String PROJECT_NAME_DEFAULT  = "demo";
	public static final String STRUCTURE_TYPE_DEFAULT  = "maven";
	
	
	public static final String VIEW_TYPE_DEFAULT  =  "both";	
	public static final String VIEW_TYPE_BOTH  =  "both";	
	public static final String VIEW_TYPE_JSTL  =  "jstl";	
	public static final String VIEW_TYPE_XML  =  "xml";	
	
	
	public static final String DB_TYPE_DEFAULT  = "oracle";	
	public static final String DB_TYPE_ORACLE  = "oracle";
	public static final String DB_TYPE_SYBASE  = "sybase";
	public static final String DB_TYPE_MSSQL  = "mssql";
	public static final String DB_TYPE_DB2  = "db2";
	public static final String DB_TYPE_MYSQL  = "mysql";
	
	public static final String SPRING_VERSION_DEFAULT  ="1";
	public static final String SPRING_VERSION_1  ="1";
	public static final String SPRING_VERSION_2  ="2";
	
	public static final String CONTROLLER_TYPE_DEFAULT = "single";
	public static final String CONTROLLER_TYPE_SINGLE = "single";
	public static final String CONTROLLER_TYPE_MULTIPLE = "multiple";
	
	public final static String GROUPID  = "groupId";
	public final static String ARTIFACTID = "artifactId";
	public final static String VERSION = "version";
	public final static String OUTPUT_DIRECTORY = "outputDirectory";
	
	public final static String DEFAULT_GROUPID  = "org.springonrails.demo";
	public final static String DEFAULT_OUTPUT_DIR = "target";
	public final static String DEFAULT_ARTIFACTID = "demo";
	public final static String DEFAULT_VERSION = "1.0.0-SNAPSHOT";
	
	private String groupId                     = null;	
	private String artifactId                  = null;
	private String version                     = null;
	private String outputDir                   = OUTPUT_DIR_DEFAULT;
	
	private String outputPackageName           = OUTPUT_PACKAGE_NAME_DEFAULT ;
	private String outputPackageNameInFilePath = OUTPUT_PACKAGE_NAME_IN_FILEPATH_DEFAULT;	
	private String projectName                 = PROJECT_NAME_DEFAULT;
	
	private String structureType               = STRUCTURE_TYPE_DEFAULT;
	private String viewType                    = VIEW_TYPE_DEFAULT;	
	private String dbType                      = DB_TYPE_DEFAULT;	
	private String springVersion 			   = SPRING_VERSION_DEFAULT;
	private String controllerType			   = CONTROLLER_TYPE_DEFAULT;
	
		
	private ApplicationStructure directoryStructure = new ApplicationStructure();
	private Properties additionalProperties = new Properties();
	
	
	
	public boolean isJdbcSetted()
	{
		return !StringUtils.isEmpty(additionalProperties.getProperty(JDBC_PROP_DRIVER)) &&
		!StringUtils.isEmpty(additionalProperties.getProperty(JDBC_PROP_URL));
	}
	
	
	public String getJdbcDriverClassName()
	{
		String prop = additionalProperties.getProperty(JDBC_PROP_DRIVER);
		if(StringUtils.isEmpty(prop))prop = "your.jdbc.driver.class.name";
		return prop;
	}
	
	public String getJdbcUrl()
	{
		String prop = additionalProperties.getProperty(JDBC_PROP_URL);
		if(StringUtils.isEmpty(prop))prop = "your.jdbc.url";
		return prop;
	}
	
	public String getJdbcUsername()
	{
		String prop = additionalProperties.getProperty(JDBC_PROP_USER);
		if(StringUtils.isEmpty(prop))prop = "your.jdbc.username";
		return prop;
	}
	
	public String getJdbcPassword()
	{
		String prop = additionalProperties.getProperty(JDBC_PROP_PASSWORD);
		if(StringUtils.isEmpty(prop))prop = "your.jdbc.password";
		return prop;
	}
	
	public String getVersion() 
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(VERSION)))
		{
				return additionalProperties.getProperty(VERSION).trim();
		}	
		if(version==null) version = DEFAULT_VERSION;		
		return version;
	}


	public void setVersion(String version) {
		this.version = version;
	}


	public String getControllerType() {
		if(!StringUtils.isEmpty(additionalProperties.getProperty(CONTROLLER_TYPE)))
		{
			controllerType = additionalProperties.getProperty(CONTROLLER_TYPE).trim();
		}
		if(controllerType==null) controllerType=CONTROLLER_TYPE_SINGLE;		
		return controllerType;
	}


	public void setControllerType(String controllerType) {
		this.controllerType = controllerType;
	}


	public AppConfig(){super();}
	
	
	public AppConfig(String outputDir, String artifactId, String groupId,			
			 Properties props) 
	{
		directoryStructure = new ApplicationMavenStructure();
		if(!isEmpty(outputDir)) this.outputDir = outputDir;
		if(!isEmpty(artifactId)) {
			this.artifactId = artifactId;
			this.projectName = artifactId;		
		}
		if(!isEmpty(groupId)) {
			this.groupId = groupId;
			this.outputPackageName = groupId;
		}
		this.outputPackageNameInFilePath = this.outputPackageName.replace('.', '/');
		additionalProperties.putAll(props);		
	}
	
	
	public String getOrmType()
	{
		String type = additionalProperties.getProperty(ORM_TYPE, ORM_TYPE_IBATIS);
		return type;
	}
	
	public int getJeeVersion()
	{
		String v = additionalProperties.getProperty(JEE_VERSION, JEE_VERSION_1_3);
		if(v.equalsIgnoreCase(JEE_VERSION_1_3) || v.equalsIgnoreCase(JEE_VERSION_3))
			return 3;
		else if(v.equalsIgnoreCase(JEE_VERSION_1_5) || v.equalsIgnoreCase(JEE_VERSION_5))
			return 5;
		else if(v.equalsIgnoreCase(JEE_VERSION_1_6) || v.equalsIgnoreCase(JEE_VERSION_6))
			return 6;
		else if(v.equalsIgnoreCase(JEE_VERSION_1_7) || v.equalsIgnoreCase(JEE_VERSION_7))
			return 7;		
		return 4;
	}
	
	public String getProperty(String name)
	{
		
		return (StringUtils.isEmpty(additionalProperties.getProperty(name)))?"":additionalProperties.getProperty(name);
		
	}
	
	
	public String getProperty(String name, String defaultValue)
	{
		return additionalProperties.getProperty(name, defaultValue);		
	}
	
	public Properties getAdditionalProperties() {
		return additionalProperties;
	}


	public void setAdditionalProperties(Properties additionalProperties) {
		this.additionalProperties = additionalProperties;
	}

  
	public String getGroupId() 
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(GROUPID)))
		{
				return additionalProperties.getProperty(GROUPID).trim();
		}		
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getOutputDirectory() 
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(OUTPUT_DIRECTORY)))
		{
				return additionalProperties.getProperty(OUTPUT_DIRECTORY).trim();
		}	
		return outputDir;
	}

	public void setOutputDirectory(String outputDir) {
		this.outputDir = outputDir;
	}

	public String getArtifactId() {
		if(!StringUtils.isEmpty(additionalProperties.getProperty(ARTIFACTID)))
		{
				return additionalProperties.getProperty(ARTIFACTID).trim();
		}
		return artifactId;
	}

	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}
	
	public boolean isSpring2()
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(SPRING_VERSION))
			&& additionalProperties.getProperty(SPRING_VERSION).equalsIgnoreCase(SPRING_VERSION_2))
		{
			return true;
		}
		return this.springVersion.equalsIgnoreCase(SPRING_VERSION_2);
	}
	
	
	public boolean isMultipleController()
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(CONTROLLER_TYPE))
			&& additionalProperties.getProperty(CONTROLLER_TYPE).equalsIgnoreCase(CONTROLLER_TYPE_MULTIPLE))
		{
			return true;
		}
		return this.controllerType.equalsIgnoreCase(CONTROLLER_TYPE_MULTIPLE);
	}
	
	public boolean isMavenStructure()
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(STRUCTURE_TYPE))
			&& additionalProperties.getProperty(STRUCTURE_TYPE).equalsIgnoreCase(STRUCTURE_TYPE_DEFAULT))
		{
			return true;
		}
		return this.structureType.equalsIgnoreCase(STRUCTURE_TYPE_DEFAULT);
	}
	
	public boolean isXmlView()
	{
		if(!StringUtils.isEmpty(additionalProperties.getProperty(VIEW_TYPE))
				&& additionalProperties.getProperty(STRUCTURE_TYPE).equalsIgnoreCase(STRUCTURE_TYPE_DEFAULT))
			{
				return true;
			}
		
		return this.viewType.equalsIgnoreCase(VIEW_TYPE_XML);
	}
	
	private boolean isEmpty(String value)
	{
		return (value==null || value.trim().length()==0);
	}
	
	public File getProjectDirectory()
	{
		File ret = new File(getOutputDir(), getProjectName());
		if(ret.exists()==false) ret.mkdirs();
		return 	ret;
	}
	
	
	
	public File getJavaSourceDirectory()
	{
		File ret = new File(getProjectDirectory(), directoryStructure.getJavaSourcePath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getWebAppDirectory()
	{
		File ret = new File(getProjectDirectory(), directoryStructure.getWebAppPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getWebResourceDirectory()
	{
		File ret = new File(getProjectDirectory(), directoryStructure.getResourcesPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getWebInfDirectory()
	{
		File ret = new File(getWebAppDirectory(), directoryStructure.getWebInfPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getSpringViewJspDirectory()
	{
		File ret = new File(getWebAppDirectory(), directoryStructure.getWebJspPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getWebClassesDirectory()
	{
		File ret = new File(getWebAppDirectory(), directoryStructure.getWebClassesPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	

	public File getDDLDirectory()
	{
		File ret = new File(getWebAppDirectory(), directoryStructure.getDdlPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	

	public File getWebLibDirectory()
	{
		File ret = new File(getWebAppDirectory(), directoryStructure.getWebLibPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaPackageDirectory()
	{
		File ret = new File(getJavaSourceDirectory(), getOutputPackageNameInFilePath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	

	public File getJavaTransDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getTransPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaViewDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getViewPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaXmlDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getXmlPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaModelDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getModelPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	public File getJavaModelMappingDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getModelMappingPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaDaoDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getDaoPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	public File getJavaDaoIbatisDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getIbatsPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaDaoHibernateDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getHibernatePath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	public File getJavaDaoMockDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getMockPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	
	
	
	public File getJavaControllerDirectory()
	{
		File ret = new File(getJavaPackageDirectory(), directoryStructure.getControllerPath());
		if(ret.exists()==false) ret.mkdirs();		
		return 	ret;
	}
	
	
	
	
	public String getOutputPackageName() 
	{		
		return getGroupId();
	}
	public void setOutputPackageName(String outputPackageName) {
		this.groupId = outputPackageName;
	}
	public String getOutputPackageNameInFilePath() {
		
		return (this.groupId!=null)?this.groupId.replace('.', '/'):".";
	}
	public void setOutputPackageNameInFilePath(String outputPackageNameInFilePath) {
		this.outputPackageNameInFilePath = outputPackageNameInFilePath;
	}
	
	public String getOutputDir() {
		String vt = additionalProperties.getProperty(OUTPUT_DIR);
		if(!StringUtils.isEmpty(vt)) return vt;		
		return outputDir;
	}
	public void setOutputDir(String outputDir) {
		this.outputDir = outputDir;
	}
	public String getProjectName() {
		String vt = additionalProperties.getProperty(ARTIFACTID);
		if(!StringUtils.isEmpty(vt)) return vt;
		return artifactId;
	}
	public void setProjectName(String projectName) {
		this.artifactId = projectName;
	}
	public String getStructureType() {
		String vt = additionalProperties.getProperty(STRUCTURE_TYPE);
		if(!StringUtils.isEmpty(vt)) return vt;
		return structureType;
	}
	public void setStructureType(String structureType) {
		this.structureType = structureType;
	}
	public String getViewType() 
	{		
		String vt = additionalProperties.getProperty(VIEW_TYPE);
		if(!StringUtils.isEmpty(vt)) return vt;
		return viewType;
	}
	public void setViewType(String viewType) {
		this.viewType = viewType;
	}
	public String getDbType() {
		String vt = additionalProperties.getProperty(DB_TYPE);
		if(!StringUtils.isEmpty(vt)) return vt;
		return dbType;
	}
	public void setDbType(String dbType) {
		this.dbType = dbType;
	}
	public String getSpringVersion() {
		String vt = additionalProperties.getProperty(SPRING_VERSION);
		if(!StringUtils.isEmpty(vt)) return vt;
		return springVersion;
	}
	public void setSpringVersion(String springVersion) {
		this.springVersion = springVersion;
	}

}
